package com.example.shop.service;

import com.example.shop.controller.dto.UserDTO;
import com.example.shop.dao.UserDao;
import com.example.shop.entity.User;
import com.example.shop.model.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * author: adnaan
 * create: 2023-11-07 13:48
 * Description:
 */
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public ApiResponse login(UserDTO userDTO) {
        if(userDTO.getUsername().isEmpty()||userDTO.getPassword().isEmpty()){
            return ApiResponse.error("400","用户名密码不能为空");
        }
        Query query=new Query(Criteria.where("username").is(userDTO.getUsername()));
        User user = userDao.get(query);
        if(user!=null&&user.getPassword().equals(userDTO.getPassword())){
            String sessionKey = "session"+user.getId();
            stringRedisTemplate.opsForValue().set(sessionKey,user.getUsername());
            UserDTO userdto = new UserDTO();
            userdto.setUsername(user.getUsername());
            userdto.setPassword(user.getPassword());

            return ApiResponse.success("200","登陆成功",userdto);
        }else{
            return ApiResponse.error("500","登陆失败，请检查用户名及密码是否正确");
        }
    }

    public ApiResponse register(User s) {
        if(s.getUsername().isEmpty()||s.getPassword().isEmpty()){
            return ApiResponse.error("500", "用户名或密码不能为空");
        }
        Criteria criteriaName = Criteria.where("username").is(s.getUsername());
        Criteria orCriteria = new Criteria();
        orCriteria.orOperator(criteriaName);
        Query query = new Query(orCriteria);
        if (mongoTemplate.count(query, User.class) == 1) {
            return ApiResponse.error("500", "用户名重复");
        }
        return ApiResponse.success("200","注册成功",userDao.save(s));
    }

    public ApiResponse loginOut(UserDTO userDTO) {
        Query query=new Query(Criteria.where("username").is(userDTO.getUsername()));
        User user = userDao.get(query);
        String sessionKey = "session"+user.getId();
        if (Boolean.FALSE.equals(stringRedisTemplate.delete(sessionKey))) {
            return ApiResponse.error("500", "未找到session" + user.getUsername());
        } else {
            return ApiResponse.success("200","退出成功");
        }
    }

    public ApiResponse updateUser(User user) {
        Query query=new Query(Criteria.where("username").is(user.getUsername()));
        Update update=new Update();
        update.set("password",user.getPassword());
        update.set("nickname",user.getNickname());
        update.set("email",user.getEmail());
        update.set("phone",user.getPhone());
        return userDao.update(query,update);
    }
}
